import Vue from 'vue'
import Vuex from 'vuex'
import * as types from './types'
import * as cache from "../common/cache";
import * as util from "../common/util";
import * as jsencrypt from "../common/jsencrypt";
import vue from "vue";
Vue.use(Vuex);



export default new Vuex.Store({
    state: {
        fileUrl: '/manage/common/files/upload',
        userInfo: cache.getUserInfo() || {},
        // 角色名
        roleName: cache.get('roleName') || '',
        // 权限集合
        permissions: cache.get('permissions') || {},

        // 平台配置信息
        conf: cache.get('conf') || {},
        // 租户配置信息
        xt: cache.get('xt') || {},
        // 当前菜单列表
        menuList: cache.get('menuList') || [],
        // 菜单回显展示
        activeUrl: cache.get('activeUrl') || '',
        parentId: cache.get('parentId') || '',
        // 货币对象
        currency: cache.get('currency') || vue.prototype.$currency,

    },
    mutations: {
        [types.LOGIN]: (state, data) => {
            localStorage.userId = data;
            state.userId = data;
        },
        [types.THREADID]: (state, data) => {
            // localStorage.threadid = data;
            state.threadid = data;
        },

        SET_USER_INFO: (state, userInfo) => {
            // console.log('vuex SET_USER_INFO', state, userInfo)
            state.userInfo = userInfo
            cache.set('userInfo', userInfo)
        },
        SET_PERMISSIONS: (state, permissions) => {
            state.permissions = util.buildPermissions(permissions)
            cache.set('permissions', state.permissions)
        },
        SET_MENU_LIST: (state, menuList) => {
            state.menuList = menuList
            cache.set('menuList', menuList)
        },
        SET_CONF: (state, conf) => {
            state.conf = util.list2mapForConfig(conf)
            cache.set('conf', state.conf)
        },
        SET_XT: (state, xt) => {
            state.xt = util.list2mapForConfig(xt)
            cache.set('xt', state.xt)
        },
        SET_CURRENCY: (state, currency) => {
            state.currency = currency
            cache.set('currency', currency)
        },
        SET_ACTIVE_URL: (state, activeUrl) => {
            state.activeUrl = activeUrl
            cache.set('activeUrl', activeUrl)
        },
        SET_ACTIVE_ID: (state, parentId) => {
            state.parentId = parentId
            cache.set('parentId', parentId)
        },
        LOGIN: (state, res) => {
            state.userInfo = res.data
            cache.setUserInfo(res.data)
            
            cache.set('platformId', res.data.platformList[0].id)

            const serve = res.data.platformList[0].serveList[0];

            cache.set('serveId', serve.serveId)
            let tempPermissions = serve.permissions
            const map = []
            tempPermissions.forEach(item => {
                map.push(jsencrypt.encrypt(encodeURIComponent(item)))
            })
            state.permissions = map
            cache.set('permissions', state.permissions)

            state.menuList = serve.menuList
            cache.set('menuList', serve.menuList)


            state.roleName = serve.roleList[0].roleName
            cache.set('roleName', jsencrypt.encrypt(state.roleName))

            //用户类型: 1-平台 2-代理 3-业务员 4-网点 5-自定义 6-商家
            cache.set('userType', jsencrypt.encrypt(encodeURIComponent(serve.roleList[0].userType)))

            //代理商等级 1，2，3
            cache.set('agentLevel', jsencrypt.encrypt(encodeURIComponent(serve.level)))
        },
        LOGOUT: (state) => {
            // console.log('vuex logout')
            state.userInfo = {}
            state.permissions = {}
            state.menuList = []
            state.activeUrl = ''
            state.parentId = ''
            let platformId =  cache.get('platformId')
            // 清除当前站点所有会话数据
            cache.clear()

            // 保存platformId
            cache.set('platformId', platformId)
        },
    },
    getters: {
        // idShow: state => state.projectId,
        // projectShow: state => state.valueId,

        userInfo: state => state.userInfo,
        roleName: state => state.roleName,
        permissions: state => state.permissions,
        menuList: state => state.menuList,
        conf: state => state.conf,
        xt: state => state.xt,
        activeUrl: state => state.activeUrl,
        parentId: state => state.parentId,
        currency: state => state.currency,

    },
    actions: {
        getprojectId(context) {
            context.commit('device');
        }
    }
})